package com.atguigu.gulimall.ware.service.impl;

import com.atguigu.common.utils.PageUtils;
import com.atguigu.common.utils.Query;
import com.atguigu.gulimall.ware.dao.PurchaseDetailDao;
import com.atguigu.gulimall.ware.entity.PurchaseDetailEntity;
import com.atguigu.gulimall.ware.service.PurchaseDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.Map;


@Service("purchaseDetailService")
public class PurchaseDetailServiceImpl extends ServiceImpl<PurchaseDetailDao, PurchaseDetailEntity> implements PurchaseDetailService {

    /**
     * 功能描述：采购详情分页查询
     *
     * @param params 分页查询条件
     * @return PageUtils 分页查询结构
     * @author cakin
     * @date 2020/11/8
     */
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        // 查询条件
        QueryWrapper<PurchaseDetailEntity> queryWrapper = new QueryWrapper<PurchaseDetailEntity>();
        // 拼接关键字检索
        String key = (String) params.get("key");
        if (!StringUtils.isEmpty(key)) {
            // purchase_id  sku_id 这两个是关键字
            queryWrapper.and(w -> {
                w.eq("purchase_id", key).or().eq("sku_id", key);
            });
        }
        // 拼接 status 检索
        String status = (String) params.get("status");
        if (!StringUtils.isEmpty(status)) {
            queryWrapper.eq("status", status);
        }
        // 拼接 wareId 检索
        String wareId = (String) params.get("wareId");
        if (!StringUtils.isEmpty(wareId)) {
            queryWrapper.eq("ware_id", wareId);
        }
        // 分页查询
        IPage<PurchaseDetailEntity> page = this.page(new Query<PurchaseDetailEntity>().getPage(params), queryWrapper);
        return new PageUtils(page);
    }

    /**
     * 功能描述：根据采购单id获得采购需求
     *
     * @param id 采购单id
     * @return List<PurchaseDetailEntity> 采购需求列表
     * @author cakin
     * @date 2020/11/10
     */
    @Override
    public List<PurchaseDetailEntity> listDetailByPurchaseId(Long id) {
        // 采购单作为条件进行查询
        List<PurchaseDetailEntity> purchaseId = this.list(new QueryWrapper<PurchaseDetailEntity>().eq("purchase_id", id));
        return purchaseId;
    }

}
